package cn.cdeden.knowledge.controller.schedule;

import cn.cdeden.common.core.domain.R;
import cn.cdeden.knowledge.domain.SysCalendarType;
import cn.cdeden.knowledge.service.SysCalendarTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
//@Api(tags = "日程类型管理")
@RequestMapping("/api/sysCalendarType")
public class SysCalendarTypeController {

    @Autowired
    private SysCalendarTypeService sysCalendarTypeService;

    @PostMapping("/sys-calendar-type/create")
//    @Log("新增日程类型")
//    @ApiOperation("新增日程类型")
    public R create (@RequestBody SysCalendarType sysCalendarType) {
        //检查是否有重复的
        List<SysCalendarType> sysCalendarTypeList = sysCalendarTypeService.list(new QueryWrapper<SysCalendarType>().eq("value", sysCalendarType.getValue()));
        if (!sysCalendarTypeList.isEmpty()) {
            return R.fail("406", "你创建的类型已经存在了");
        }
        List<SysCalendarType> sysCalendarTypes = sysCalendarTypeService.list(new QueryWrapper<SysCalendarType>().eq("name", sysCalendarType.getName()));
        if (!sysCalendarTypes.isEmpty()) {
            return R.fail("406", "你创建的类型已经存在了");
        }
        return R.ok(sysCalendarTypeService.save(sysCalendarType));
    }


    @GetMapping("/get-all-sys-calendar-types")
//    @Log("查询所有日程类型")
//    @ApiOperation("查询所有日程类型")
    public R<List<SysCalendarType>> getAll(@RequestParam Map<String, Object> parameters) {
        String name = MapUtils.getString(parameters, "name");
        QueryWrapper<SysCalendarType> sysCalendarTypeQueryWrapper = new QueryWrapper<>();
        if (name != null && name != "") {
            sysCalendarTypeQueryWrapper.like("name", "%" + name + "%");
        }
        return R.ok(sysCalendarTypeService.list(sysCalendarTypeQueryWrapper));
    }

    @DeleteMapping("/delete-sys-calendar-type")
//    @Log("删除日程类型")
//    @ApiOperation("删除日程类型")
    public R delete (@RequestParam("id") Long sysCalendarTypeId) {
        return R.ok(sysCalendarTypeService.removeById(sysCalendarTypeId));
    }

    @PutMapping("update-sys-calendar-type")
//    @Log("修改日程类型")
//    @ApiOperation("修改日程类型")
    public R update(@RequestBody SysCalendarType sysCalendarType) {
        List<SysCalendarType> sysCalendarTypeList = sysCalendarTypeService.list(new QueryWrapper<SysCalendarType>()
                .eq("name", sysCalendarType.getName())
                .or()
                .eq("value", sysCalendarType.getValue())
                .ne("id", sysCalendarType.getId()));
        if (!sysCalendarTypeList.isEmpty()) {
            return R.fail("406", "你修改提交的类型已经存在了");
        }
        return R.ok(sysCalendarTypeService.updateById(sysCalendarType));
    }
}
